# -*- coding: utf-8 -*-
# tasks.py
import time,os
from flask import Blueprint,render_template,request,redirect
from celery import Celery

task = Blueprint('task', __name__)

celery = Celery(
    'tasks',
    broker='redis://10.10.100.21:6379/0',
    #backend='redis://localhost:6379/0',
     )

@celery.task(name='sendmail')
def sendmail(mail):
    print('sending mail to %s...' % mail['to'])
    #time.sleep(2.0)
    print('mail sent.')
    return mail['to']



#发邮件
@task.route('/send/',methods=['GET','POST'])
def tasks_sendmail():
    if request.method=='GET':
        return render_template('admin/send.html')

    if request.method=='POST':
        email=request.form.get('email')
        if email:
            #tasks.sendmail({'to':email})
            ret=sendmail.delay({'to': email})
            print ret
        return redirect('/task/send')

if __name__=="__main__":
    #Worker
    #task='celery -A tasks worker -c 3 --loglevel=info'
    #os.system(task)
    #sendmail.delay(dict(to='jk409o@qq.com'))
    for i in range(1,10):
        ret=sendmail.delay({'to':i})
        print ret,'----->',i

